<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar fixed>
      <van-button
        slot="title"
        icon="search"
        type="info"
        class="search-btn"
        size="small"
        round
        to="/search"
      >
        搜索
      </van-button>
    </van-nav-bar>

    <!-- 频道列表 -->
    <van-tabs animated swipeable class="channel-tabs" v-model="active">
      <van-tab :title="item.name" v-for="item in channelsList" :key="item.id">
        <article-list :channel="item" />
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="iconfont icongengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
      close-icon-position="top-left"
    >
      <channel-edit
        :MyChannel="channelsList"
        :active="active"
        @update-active="onUpdateActive"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '../../api/user'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

import ArticleList from './components/ArticleList'
import ChannelEdit from './components/ChannelEdit'

export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  data() {
    return {
      active: 0, // 当前点击索引
      channelsList: [], // 频道列表数据
      isChannelEditShow: false // 控制频道编辑弹出层隐藏显示
    }
  },
  created() {
    this.loadChannels()
  },
  methods: {
    async loadChannels() {
      try {
        // const { data: res } = await getUserChannels()
        // // console.log(res)
        // this.channelsList = res.data.channels

        let channels = []
        if (this.user) {
          // 如果用户已登录 则从远端获取频道数据
          const { data: res } = await getUserChannels()
          channels = res.data.channels
        } else {
          // 非登录状态
          const localChannels = getItem('TOUTIAO_CHANNEL')
          if (localChannels) {
            // 先判断本地是否有频道数据 如果有拿出来用
            channels = localChannels
          } else {
            // 如果没有 就请求获取默认推荐的频道列表
            const { data: res } = await getUserChannels()
            channels = res.data.channels
          }
        }

        this.channelsList = channels
      } catch (error) {
        console.log('获取频道列表数据失败!')
      }
    },
    // 更新当前的频道
    onUpdateActive(index, isChannelEditShow = true) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  margin-bottom: 100px;

  /deep/ .van-nav-bar__title {
    max-width: unset;

    .search-btn {
      width: 555px;
      height: 64px;
      background-color: #5babfb;
      border: none;
      font-size: 28px;

      .van-icon {
        font-size: 32px;
      }
    }
  }

  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }

    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
  }

  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.iconfont {
      font-size: 33px;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
